﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
using System.Data.Objects;
using Exhibition.Common.Helper;
using System.Data;
namespace Exhibition.Dao
{

    public class UserDao
    {


        public UserDao()
        {

            Mapper.CreateMap<E_User, UserModel>();
        }
        public List<E_User> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_User.Count();

                return edm.E_User.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public UserModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_User.FirstOrDefault(s => s.Id == id);
                    if(query!=null){
                        var um= edm.E_UserComp.FirstOrDefault(s=>s.UserId==query.Id);
                       
                        UserModel umd=Mapper.Map<E_User,UserModel>(query);
                        if(umd!=null){
                            if(um!=null){
                                umd.compMdl=Mapper.Map<E_UserComp,UserCompModel>(um);
                            }
                        }
                       return umd;


                    }        
                return null;
            }

        }

        public List<UserModel> GetUserList(string strWhr, int tpid, int startIndex, int maxRow, out int recordcount)
        {
            var queryStr = @"select * from e_user where 1=1 ";
            if (!string.IsNullOrEmpty(strWhr))
            {
                queryStr = queryStr + strWhr;
            }
            if (tpid != 0)
            {
                queryStr = queryStr + " and UserType=" + tpid;
            }
            DataTable dt=   DBHelper.ExecuteDataSet(queryStr); 
            if(dt.Rows.Count>0){
                var query = from s in dt.AsEnumerable()
                            select new UserModel() { 
                                Id=s.Field<int>("id"),
                                AreaId = s.Field<int>("AreaId"),
                                AreaPid = s.Field<int>("AreaPid"),
                                CompName = s.Field<string>("CompName"),
                                ContactType = s.Field<string>("ContactType"),
                                CreateTime = s.Field<DateTime>("CreateTime"),
                                CreateUserId = s.Field<int>("CreateUserId"),
                                Depart = s.Field<string>("Depart"),
                                Email = s.Field<string>("Email"),
                                InCome = s.Field<string>("InCome"),
                                IsCheck = s.Field<int>("IsCheck"),
                                IsDelete = s.Field<int>("IsDelete"),
                                IsMember = s.Field<int>("IsMember"),
                                Mobile = s.Field<string>("Mobile"),
                                Nationality = s.Field<string>("Nationality"),
                                PassWord = s.Field<string>("PassWord"),
                                Position = s.Field<string>("Position"),
                                Properties = s.Field<string>("Properties"),
                                RealName = s.Field<string>("RealName"),
                                Sex = s.Field<int>("Sex"),
                                SiteID = s.Field<int>("SiteID"),
                                UserAddress = s.Field<string>("UserAddress"),
                                UserName = s.Field<string>("UserName"),
                                UserType = s.Field<int>("UserType"),
                                verifycode = s.Field<string>("verifycode"),
                                UserLevel = s.Field<int>("UserLevel") 


                            };
                recordcount = query.Count();
                return query.ToList();

            }
            recordcount = 0;
            return null;


        }



        public UserModel UserLogin(string uname, string pwd)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
               // return Mapper.Map<E_User, UserModel>(edm.E_User.FirstOrDefault(s => s.UserName == uname && s.PassWord == pwd && s.IsDelete == 0));

                var query = edm.E_User.FirstOrDefault(s => s.UserName == uname && s.PassWord == pwd && s.IsDelete == 0);
                    if(query!=null){
                        var um= edm.E_UserComp.FirstOrDefault(s=>s.UserId==query.Id);
                       
                        UserModel umd=Mapper.Map<E_User,UserModel>(query);
                        if(umd!=null){
                            if(um!=null){
                                umd.compMdl=Mapper.Map<E_UserComp,UserCompModel>(um);
                            }
                        }
                       return umd;


                    }        
                return null;

            }
        }


        public UserModel GetByEMail(string mail)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                UserModel umd = Mapper.Map<E_User, UserModel>(edm.E_User.FirstOrDefault(s => s.Email == mail));
               
                return umd;

            }
        }
        /// <summary>
        /// 邮箱验证
        /// </summary>
        /// <param name="vcode"></param>
        /// <returns></returns>
        public UserModel GetUserByCode(string vcode)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
               var umd= Mapper.Map<E_User, UserModel>(edm.E_User.FirstOrDefault(s => s.verifycode == vcode));

               if (umd != null)
               {
                   umd.IsCheck = 1;
                   Save(umd);
               }
               return umd;
            }
        }




        public int Save(UserModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_User.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_User();
                    query.CreateTime = DateTime.Now;
                    query.CreateUserId = obj.Id;
                    edm.AddToE_User(query);

                }
               
                query.UserName = obj.UserName;

                query.PassWord = obj.PassWord;

                query.CompName = obj.CompName;

                query.Sex = obj.Sex;

                query.Email = obj.Email;

                query.Mobile = obj.Mobile;

                query.AreaPid = obj.AreaPid;

                query.AreaId = obj.AreaId;

                query.RealName = obj.RealName;

                query.Nationality = obj.Nationality;

                query.Properties = obj.Properties;

                query.CreateTime = obj.CreateTime;

                query.InCome = obj.InCome;

                query.UserAddress = obj.UserAddress;

                query.IsDelete = obj.IsDelete;

                query.UserType = obj.UserType;

                query.CreateUserId = obj.CreateUserId;

                query.SiteID = obj.SiteID;

                query.IsCheck = obj.IsCheck;

                query.IsMember = obj.IsMember;

                query.verifycode = obj.verifycode;

                query.Depart = obj.Depart; 
                query.Position = obj.Position;
                query.UserLevel = obj.UserLevel; 
                query.ContactType = obj.ContactType;
                edm.SaveChanges();
                if (obj.compMdl != null)
                {
                    var cpm = edm.E_UserComp.FirstOrDefault(s => s.UserId == obj.Id);
                    if (cpm == null)
                    {
                        cpm = new E_UserComp();
                        cpm.CreateTime = DateTime.Now;

                        edm.AddToE_UserComp(cpm);
                    }
                    cpm.UserId = query.Id;
                    cpm.Lincense = obj.compMdl.Lincense;
                    cpm.CompAddr = obj.compMdl.CompAddr;
                    cpm.CompEname = obj.compMdl.CompEname;
                    cpm.CompLevel = obj.compMdl.CompLevel;
                    cpm.CompLogo = obj.compMdl.CompLogo;
                    cpm.CompName = obj.compMdl.CompName;
                    cpm.Compnature = obj.compMdl.Compnature;
                    cpm.CompSite = obj.compMdl.CompSite;
                    cpm.Contact = obj.compMdl.Contact;
                    cpm.CountryCode = obj.compMdl.CountryCode;
                    cpm.PhoneNum = obj.compMdl.PhoneNum;
                    cpm.PhoneStart = obj.compMdl.PhoneStart;
                    cpm.PostCode = obj.compMdl.PostCode;
                    cpm.ProductIds = obj.compMdl.ProductIds;
                    cpm.RegMoney = obj.compMdl.RegMoney;
                    cpm.RegYear = obj.compMdl.RegYear;
                    cpm.SiteID = obj.compMdl.SiteID;
                    cpm.UserId = obj.compMdl.UserId;
                    cpm.UserNum = obj.compMdl.UserNum;
                    cpm.GetTicket = obj.compMdl.GetTicket;
                    cpm.IsDelete = obj.compMdl.IsDelete;
                    cpm.FaxNum = obj.compMdl.FaxNum;
                    cpm.FaxStart = obj.compMdl.FaxStart;
                    cpm.OtherCountry = obj.compMdl.OtherCountry;
                }
                edm.SaveChanges();
                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_User.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

